import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
// /* 一级路由 */
// import Home from "@/pages/home";
// import Search from "@/pages/search";
// import Login from "@/pages/login";
// import Detail from "@/pages/detail";
// import Register from "@/pages/register";
// import AddCartSuccess from "@/pages/addCartSuccess";
// import Shopcart from "@/pages/shopCart";
// import Trade from "@/pages/trade";
// import PaySuccess from "@/pages/paySuccess";
// import Center from "@/pages/center";
let push = VueRouter.prototype.push;

/* 二级路由 */
// import TeamOrder from "@/pages/center/teamorder";
// import MyOrder from "@/pages/center/myorder";
VueRouter.prototype.push = function (location) {
  push.call(
    this,
    location,
    () => {},
    () => {}
  );
};
export default new VueRouter({
  mode: "hash",
  //滚动行为
  //当有路由切换的时候会执行
  scrollBehavior(to, from, savePosition) {
    //次函数需要返回一个对象:控制滚动条位置
    //对象两个属性:x(水平)、y(垂直)
    //如果属性值为负数，起始还是归0
    return {
      y: 0,
    };
  },
  routes: [
    {
      path: "/home",
      component: () => import("@/pages/home"),
      meta: {
        show: true,
        title: "首页",
      },
    },
    {
      name: "search",
      path: "/search/:keyword?",
      component: () => import("@/pages/search"),
      meta: {
        show: true,
        title: "搜索",
      },
    },
    {
      path: "/login",
      component: () => import("@/pages/login"),
      meta: {
        show: false,
        title: "登录",
      },
    },
    {
      path: "/register",
      component: () => import("@/pages/register"),
      meta: {
        show: false,
        title: "注册",
      },
    },
    {
      name: "detail",
      path: "/detail/:skuId?",
      component: () => import("@/pages/detail"),
      meta: {
        show: true,
        title: "详情",
      },
    },
    {
      name: "cartsuccess",
      path: "/addcartsuccess/:skuNum?",
      component: () => import("@/pages/addCartSuccess"),
      meta: {
        show: true,
        title: "加入购物车",
      },
    },
    {
      path: "/shopcart",
      component: () => import("@/pages/shopCart"),
      meta: {
        show: true,
        title: "购物车",
      },
    },
    {
      path: "/trade",
      component: () => import("@/pages/trade"),
      meta: {
        show: true,
        title: "交易",
      },
      beforeEnter: (to, from, next) => {
        if (from.path == "/shopcart") {
          next();
        } else {
          next(false);
        }
      },
    },
    {
      path: "/pay",
      component: () => import("@/pages/pay"),
      meta: {
        title: "支付",
        show: true,
      },
      beforeEnter: (to, from, next) => {
        if (from.path == "/trade") {
          next();
        } else {
          next(false);
        }
      },
    },
    {
      path: "/paysuccess",
      component: () => import("@/pages/paySuccess"),
      meta: {
        title: "支付成功",
        show: true,
      },
    },
    {
      path: "/center",
      component: () => import("@/pages/center"),
      meta: {
        show: true,
        title: "个人中心",
      },
      children: [
        {
          path: "teamorder",
          component: () => import("@/pages/center/teamorder"),
          meta: {
            title: "团购订单",
            show: true,
          },
        },
        {
          path: "myorder",
          component: () => import("@/pages/center/myorder"),
          meta: {
            show: true,
            title: "我的订单",
          },
        },
        {
          path: "/",
          redirect: "myorder",
        },
      ],
    },
    {
      path: "/",
      redirect: "/home",
    },
  ],
});
